package com.common.help.test08;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class MultiServerSocket {

	
	private static final int count = 1<<2;
	public static void main(String[] args) throws IOException {
		 
		ServerSocket server = new ServerSocket(ServerSocket1.port);
		int i=0;
		try {
			while(true){
				Socket socket = server.accept();
				new ServerSocketThread(socket);
				if(++i>count)break;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			System.out.println(i);
			server.close();
		}
	}
}
class ServerSocketThread extends Thread{
	private Socket socket;
	private PrintWriter pw;
	private BufferedReader br;
	ServerSocketThread(){}
	ServerSocketThread(Socket socket){
		try {
			this.socket = socket;
			pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())));
			br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
			start();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	@Override
	public void run() {
		String line ="";
		while(true){
			try {
				line = br.readLine();
				if(line.equals("END")){
					break;
				}
				System.err.println(line);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		try {
			socket.close();
			pw.close();
			br.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
}
